﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace WisdriContacts
{
    using System.Collections.ObjectModel;
    using System.IO.IsolatedStorage;

    using LLSSample;

    using WisdriContacts.Model;

    public partial class ContactListPage : PhoneApplicationPage
    {
        public ContactListPage()
        {
            InitializeComponent();

            this.DataContext = App.ViewModel;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string dept = "";
            string filterString = "";
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out dept))
            {
                App.ViewModel.PersonList.Clear();
                App.ViewModel.Items.Where(p => p.DEPARTMENT == dept)
                    .ToList()
                    .ForEach(p => App.ViewModel.PersonList.Add(p));
            }

            if (NavigationContext.QueryString.TryGetValue("filter", out filterString))
            {
                App.ViewModel.PersonList.Clear();

                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                bool isAdmin;
                if (!settings.TryGetValue("IsAdmin", out isAdmin))
                {
                    isAdmin = false;
                }

                var filters = filterString.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                var tempItems = new List<PERSON>(App.ViewModel.Items);
                foreach (var filter in filters)
                {
                    tempItems = tempItems.Where(p =>
                        p.ID.Contains(filter) ||
                        p.DEPARTMENT.Contains(filter) ||
                        p.MOBILE_PHONE.Contains(filter) ||
                        p.NAME.Contains(filter) ||
                        p.OFFICE_PHONE.Contains(filter) ||
                        (isAdmin && p.CAR.ToLower().Contains(filter.ToLower())) ||
                        (filter.All(x => char.IsLetter(x)) && p.PY.ToLower().Contains(filter.ToLower()))
                        ).OrderBy(p => p.NAME).ToList();
                }
                tempItems.ForEach(p => App.ViewModel.PersonList.Add(p));
            }

            App.ViewModel.PersonListByGroup.Clear();
            AlphaKeyGroup<PERSON>.CreateGroups(
                App.ViewModel.PersonList,
                System.Threading.Thread.CurrentThread.CurrentUICulture,
                s => s.NAME,
                true).ForEach(p => App.ViewModel.PersonListByGroup.Add(p));

            base.OnNavigatedTo(e);
        }

        private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 如果所选项为空(没有选定内容)，则不执行任何操作
            if (MainLongListSelector.SelectedItem == null)
                return;

            // 导航到新页面
            NavigationService.Navigate(
                new Uri("/DetailsPage.xaml?selectedItem=" + (MainLongListSelector.SelectedItem as PERSON).ID,
                    UriKind.Relative));

            // 将所选项重置为 null (没有选定内容)
            MainLongListSelector.SelectedItem = null;
        }
    }
}